<template>
	<view>
		<j-cell-group>
			<j-cell title="开启" :more="false">
				<view slot="tips">
					<switch ref="swb" color="#FF0025" :checked="DetectMotionDetectList[channel].Enable"
						@change="bindchangeEnable">
					</switch>
				</view>
			</j-cell>
			<j-cell title="人形检测" :more="false"
				v-if="DetectMotionDetectList[channel].Enable && ability.AlarmFunction.PEAInHumanPed">
				<view slot="tips">
					<!-- <switch :checked="HumanDetectionEnable" @change="HumanDetectionChange"></switch> -->
					<j-switch v-model="HumanDetectionEnable" @change="HumanDetectionChange"></j-switch>
				</view>
			</j-cell>
			<j-cell-group title="报警设置" v-if="DetectMotionDetectList[channel].Enable">
				<j-cell-card title="报警方式" :show="false" :is-touch="false" border style="background: #f5f7fb">
					<template v-for="(item,index) in typeAlarm">
						<!-- <checkbox-group @change="alarmChange"> -->
						<j-cell-card :key="index" :is-touch="false" v-if="item.type != 'VoiceEnable'" :tips="item.tips"
							:title="item.name" :more="item.more">
							<view slot="icon">
								<checkbox :value="item.type" @click="alarmChange(item)" :checked="item.check"
									style="transform:scale(0.7)" />
							</view>
							<template>
								<view class="laba" v-if="item.name == '警示灯'">
									<slider min="1" max="120" style="width: 80%;"
										:value="DetectMotionDetectList[channel].EventHandler.AlarmOutLatch"
										@change="AlarmlightChange" show-value />
								</view>
							</template>
						</j-cell-card>
						<j-cell v-if="item.type == 'VoiceEnable' && ability.OtherFunction.SupportAlarmVoiceTips"
							:title="item.name" :tips="item.tips" :more="item.more"
							@click.stop.native="toWarningTone(index)">
							<view slot="icon">
								<checkbox :value="item.type" @click.stop="alarmChange(item)" :checked="item.check"
									style="transform:scale(0.7)" />
							</view>
						</j-cell>
						<!-- </checkbox-group> -->
					</template>
				</j-cell-card>
			</j-cell-group>
			<!-- 高级 -->
			<j-cell-group title="其他设置" v-if="DetectMotionDetectList[channel].Enable">
				<j-cell-card title="高级" :show="true" :is-touch="false" border style="background: #f5f7fb">
					<j-cell v-if="HumanDetectionEnable" title="显示智能踪迹(人形检测)" :more="false">
						<view slot="tips">
							<switch color="#FF0025" :checked="ShowTrack" @change="ShowTrackChange">
							</switch>
						</view>
					</j-cell>
					<j-cell-card title="报警间隔" :show="false" :is-touch="false" :tips="interval" border
						style="background: #f5f7fb">
						<template>
							<view class="laba">
								<slider min="10" max="1800" :show-value="false" style="width: 90%;" :value="volume"
									@change="intervalChange" />
							</view>
						</template>
					</j-cell-card>
					<j-cell title="报警时段" :tips="timetips" :show="false" :to="'/jvss/pagesDevice/device/device-live/setting/IPolice/alarmTime?token=' + token
						 + '&deviceName=' + device.deviceName + '&deviceNo=' + device.deviceNo">
					</j-cell>
					<j-cell-card title="灵敏度" :tips="levels[DetectMotionDetectList[channel].Level - 1].name"
						:show="false" :is-touch="false" border style="background: #f5f7fb">
						<template v-for="(item, index) in levels">
							<j-cell :key="index" :title="item.name" :more="false" @jftap="bindchangeLevels(item)">
								<view slot="tips" v-if="DetectMotionDetectList[channel].Level === levels[index].value">
									<uni-icons custom-prefix="iconfont" type="icon-duihao" size="24" color="#FF0025">
									</uni-icons>
								</view>
							</j-cell>
						</template>
					</j-cell-card>
				</j-cell-card>
			</j-cell-group>
		</j-cell-group>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				device: {},
				DetectMotionDetectList: [],
				channel: 0,
				token: '',
				timeMaskList: [],
				volume: 0,
				lightVolume: 0,
				interval: 0,
				HumanDetectionEnable: false,
				ShowTrack: false,
				TrackingShow: false,
				levels: [{
						value: 1,
						name: '最低'
					},
					{
						value: 2,
						name: '较低'
					},
					{
						value: 3,
						name: '中'
					},
					{
						value: 4,
						name: '较高'
					},
					{
						value: 5,
						name: '很高'
					},
					{
						value: 6,
						name: '最高'
					}
				],
				typeAlarm: [{
					name: '抓拍照片',
					type: 'SnapEnable',
					more: false,
					check: false,
					tips: ''
				}, {
					name: '抓录视频',
					more: false,
					type: 'RecordEnable',
					check: false,
					tips: ''
				}, {
					name: '设备警铃',
					more: true,
					type: 'VoiceEnable',
					check: false,
				}, {
					name: '警示灯',
					more: true,
					type: 'AlarmOutEnable',
					check: false,
					tips: ''
				}, ],
				timetips: '',
				ability: '', //能力集
			}
		},
		onLoad(options) {
			console.log(options)
			this.device = options
			this.token = options.token
			this.ability = JSON.parse(options.ability);
			console.log(this.ability)
			this.getconfig('Detect.MotionDetect');
			this.HumanDetection(); //人形检测
			this.NetWorkPMS();
			this.DetectTrack(); //移动侦测
		},
		methods: {
			HumanDetection() {
				this.getconfig('Detect.HumanDetection');
			},
			NetWorkPMS() {
				this.getconfig('NetWork.PMS');
			},
			DetectTrack() {
				this.getconfig('Detect.DetectTrack');
			},
			// 获取配置
			getconfig(v) {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: v
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading()
					console.log(result);
					if (v == 'Detect.MotionDetect') {
						this.DetectMotionDetectList = result['Detect.MotionDetect']
						let obj = result['Detect.MotionDetect'][0].EventHandler
						this.typeAlarm[3].tips = result['Detect.MotionDetect'][0].EventHandler.AlarmOutLatch + '秒'
						console.log(obj)
						this.typeAlarm.forEach(item => {
							Object.keys(obj).forEach(key => {
								if (item.type == key) {
									console.log(key)
									item.check = obj[key]
								}
							})
						})
						result['Detect.MotionDetect'][0].EventHandler.TimeSection.forEach(item => {
							if (item[0].substring(0, 1) == '1') {
								this.timetips = '全天报警'
							} else {
								this.timetips = '自定义报警'
							}
						})
						this.voicePrompt()
					}
					if (v == 'Detect.HumanDetection') {
						this.HumanDetectionEnable = result['Detect.HumanDetection'][0].Enable
						this.ShowTrack = result['Detect.HumanDetection'][0].ShowTrack
						console.log(this.HumanDetectionEnable, '状态')
					}
					if (v == 'NetWork.PMS') {
						this.intervalFilter(result['NetWork.PMS'].PushInterval)
						this.volume = result['NetWork.PMS'].PushInterval
					}
					if (v == 'Detect.DetectTrack') {
						this.TrackingShow = result['Detect.DetectTrack'].Enable;
					}
				});
			},
			// 获取警铃声
			voicePrompt() {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Ability.VoiceTipType'
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading();
					let voicePrompt = result['Ability.VoiceTipType'].VoiceTip.find(ele => ele.VoiceEnum == this
						.DetectMotionDetectList[0].EventHandler.VoiceType)
					console.log(voicePrompt, '获取警铃声')
					this.typeAlarm[2].tips = voicePrompt.VoiceText;
					this.$forceUpdate();
				});
			},
			// 警示灯时间
			AlarmlightChange(v) {
				this.DetectMotionDetectList[this.channel].EventHandler.AlarmOutLatch = v.detail.value;
				this.typeAlarm[3].tips = v.detail.value + '秒'
				this.setconfig()
			},
			// 报警方式
			alarmChange(v) {
				console.log(v)
				Object.keys(this.DetectMotionDetectList[this.channel].EventHandler).forEach(key => {
					if (v.type == key) {
						this.DetectMotionDetectList[this.channel].EventHandler[key] = !v.check
					}
				})
				this.typeAlarm.forEach(item => {
					if (v.type == item.type) {
						item.check = !item.check
					}
				})
				if (v.type == 'RecordEnable' && v.check) {
					this.DetectMotionDetectList[this.channel].EventHandler.RecordMask = '0x1'
				} else if (v.type == 'RecordEnable' && !v.check) {
					this.DetectMotionDetectList[this.channel].EventHandler.RecordMask = '0x0'
				} else if (v.type == 'SnapEnable' && v.check) {
					this.DetectMotionDetectList[this.channel].EventHandler.SnapShotMask = '0x1'
				} else if (v.type == 'SnapEnable' && !v.check) {
					this.DetectMotionDetectList[this.channel].EventHandler.SnapShotMask = '0x0'
				}
				console.log(this.DetectMotionDetectList[this.channel].EventHandler.RecordMask)
				this.setconfig()
			},
			lightChange(v) {
				this.typeAlarm.forEach(item => {
					if (item.name == '警示灯') item.tips = v.detail.value + '秒'
				})
			},
			// 报警间隔滑动change事件
			intervalChange(v) {
				console.log(v)
				this.volume = v.detail.value
				this.intervalFilter(v.detail.value, 'set');
			},
			// 间隔时间处理
			intervalFilter(v, r) {
				let result = parseInt(v);
				let m =
					Math.floor((result / 60) % 60) < 10 ?
					"0" + Math.floor((result / 60) % 60) :
					Math.floor((result / 60) % 60);
				let s =
					Math.floor(result % 60) < 10 ?
					"0" + Math.floor(result % 60) :
					Math.floor(result % 60);
				let res = "";
				if (m !== "00") res += `${m}:${s}`;
				if (m == "00") res += `00:${s}`;
				this.interval = res;
				if (r == 'set') {
					this.setconfig('NetWork.PMS')
				}
				console.log(res)
			},
			// 是否开启移动侦测
			bindchangeEnable(ev) {
				this.DetectMotionDetectList[this.channel].Enable = ev.detail.value;
				this.setconfig()
			},
			// 人形检测设置
			HumanDetectionChange(v) {
				console.log(v)
				if (v && this.TrackingShow) {
					uni.showModal({
						title: '提示',
						content: `人形检测和移动追踪不能同时打开，如果开启人形检测，移动追踪将会被关闭，确认操作？`,
						success: (res) => {
							if (res.confirm) {
								this.TrackingShow = false;
								this.HumanDetectionEnable = true;
								this.setconfig('Detect.HumanDetection')
								this.DetectTrack(); //移动侦测
							} else if (res.cancel) {
								console.log('点击')
								this.HumanDetectionEnable = false
								console.log(this.HumanDetectionEnable)
								this.$forceUpdate();
							}
						}
					})
				} else if (v && !this.TrackingShow) {
					this.HumanDetectionEnable = true;
					this.setconfig('Detect.HumanDetection')
				} else {
					this.HumanDetectionEnable = false
					this.setconfig('Detect.HumanDetection')
				}
				console.log(this.HumanDetectionEnable, '状态')
			},
			ShowTrackChange(ev) {
				this.ShowTrack = ev.detail.value
				this.setconfig('Detect.HumanDetection')
			},
			// 灵敏度
			bindchangeLevels(ev) {
				this.DetectMotionDetectList[this.channel].Level = Number(ev.value);
			},
			// 设置配置
			setconfig(v) {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect',
					'Detect.MotionDetect': this.DetectMotionDetectList
				};
				if (v == 'Detect.HumanDetection') {
					data = {
						Name: 'Detect.HumanDetection',
						'Detect.HumanDetection': [{
							Enable: this.HumanDetectionEnable,
							ShowTrack: this.ShowTrack
						}]
					};
				}
				if (v == 'NetWork.PMS') {
					console.log(this.volume)
					data = {
						Name: v,
						'NetWork.PMS': {
							PushInterval: this.volume,
						}
					};
				}
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.showToast({
						title: '保存成功'
					});
				});
			},
			toWarningTone() {
				uni.navigateTo({
					url: '/jvss/pagesDevice/device/device-live/setting/IPolice/warningTone?token=' + this.device
						.token + '&deviceName=' + this.device.deviceName + '&deviceNo=' + this.device.deviceNo
				});
			}
		}
	}
</script>

<style lang="scss">

</style>
